<template>
  <div class="flex flex-col h-full">
    <SearchContainer @reset="() => formRef.resetFields()" @search="getData">
      <a-form ref="formRef" class="flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="zjm">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="药品编号/药品名称/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <BaseTable :scroll="{ x: 3700 }" column-code="000837" fill :loading="loading" :data-source="tableData" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex == 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'insureStatus'">
            <a-switch :checked="record.insureStatus" :un-checked-value="0" :checked-value="1" @change="onChangeInsureStatus(record)" />
          </template>
          <template v-if="column.dataIndex === 'limitFlag'">
            {{ record.limitFlag === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'specialFlag'">
            {{ record.specialFlag === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'subFlag'">
            {{ record.subFlag === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'injuryFlag'">
            {{ record.injuryFlag === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex === 'compoundFlag'">
            {{ record.compoundFlag === 1 ? '是' : '否' }}
          </template>
          <template v-if="column.dataIndex == 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg, cj: record?.cj }" />
          </template>
          <template v-if="column.dataIndex == 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)">维护</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" :pharmacology-list="pharmacologyList" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
// import { post } from '@/utils/request'
import Edit from './Edit'

const editOpen = ref(false)
const formRef = ref(null)
const formState = ref({})
const rowData = ref({})
const pharmacologyList = ref([])

const onHandleEdit = (row = {}) => {
  rowData.value = { ...row }
  editOpen.value = true
}
const onChangeInsureStatus = async (row) => {
  const status = row.insureStatus === 1 ? 0 : 1
  const { code, msg } = await post('/operation/opex/shanghai/doSaveShmiDictDrug', { id: row.id, bh: row.bh, insureStatus: status })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  getData()
}
const { loading, tableData, getData, pagination } = useTable({
  url: '/operation/opex/shanghai/getShmiDictDrugList',
  immediate: true,
  formatParams: () => {
    return {
      cxxm: 1,
      ...formState.value
    }
  }
})
</script>
<style lang="less" scoped></style>
